-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathUpdate-ScheduledTask.ps1
125 lines (96 loc) · 4.15 KB
/
Update-ScheduledTask.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<#
$Metadata = @{
Title = "Update Scheduled Task"
Filename = "Update-ScheduledTask.ps1"
Description = ""
Tags = "powershell, function, update, task"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-10-07"
LastEditDate = "2013-12-13"
Version = "2.0.0"
License = @'
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
'@
}
#>
function Update-ScheduledTask{
<#
.SYNOPSIS
Updates Windows scheduled tasks.
.DESCRIPTION
Updates Windows scheduled tasks depending on configuration files.
.EXAMPLE
PS C:\> Update-ScheduledTask
.NOTES
Requirments: PowerShell Profile Installation.
#>
param(
[Parameter(Position=0, Mandatory=$false)]
$Name,
[switch]
$All
)
if(-not ($All -or $Name)){
throw "Provide at least one parameter for this function."
}
#--------------------------------------------------#
# functions
#--------------------------------------------------#
function Get-WindowsScheduledTasks{
param(
$Folder
)
# get tasks
$Folder.GetTasks(0) | Foreach-Object {
New-Object -TypeName PSCustomObject -Property @{
'Name' = $_.Name
'Path' = $_.Path
'State' = $_.State
'Enabled' = $_.Enabled
'LastRunTime' = $_.LastRunTime
'LastTaskResult' = $_.LastTaskResult
'NumberOfMissedRuns' = $_.NumberOfMissedRuns
'NextRunTime' = $_.NextRunTime
'Author' = ([xml]$_.xml).Task.RegistrationInfo.Author
'UserId' = ([xml]$_.xml).Task.Principals.Principal.UserID
'Description' = ([xml]$_.xml).Task.RegistrationInfo.Description
}
}
# check for subfolders and their tasks
$Folder.getfolders(1) | %{Get-WindowsScheduledTasks -Folder $_}
}
#--------------------------------------------------#
# main
#--------------------------------------------------#
# Read configuration files
$ScheduledTasks = Get-ChildItem -Path $PSconfigs.Path -Filter $PSconfigs.Task.Filter -Recurse
# get existing tasks
$WindowsScheduledTasks = new-object -com("Schedule.Service")
($WindowsScheduledTasks).connect($env:COMPUTERNAME)
$WindowsScheduledTasks = Get-WindowsScheduledTasks -Folder $WindowsScheduledTasks.getfolder("\")
$ScheduledTasks | %{
$Task = [xml]$(get-content $_.FullName)
$FilePathTemp = $_.FullName + "temp.xml"
$Task | where{$All -or $Name -eq $_.Task.RegistrationInfo.Description} | %{
# resolve PowerShell variables
if($Task.Task.Actions.Exec.Command.contains("$")){$Task.Task.Actions.Exec.Command = Invoke-Expression $Task.Task.Actions.Exec.Command}
if($Task.Task.Actions.Exec.Arguments.contains("$")){$Task.Task.Actions.Exec.Arguments = Invoke-Expression $Task.Task.Actions.Exec.Arguments}
if($Task.Task.Actions.Exec.WorkingDirectory.contains("$")){$Task.Task.Actions.Exec.WorkingDirectory = [string](Invoke-Expression $Task.Task.Actions.Exec.WorkingDirectory)}
$Task.Save($FilePathTemp)
$Title = $Task.task.RegistrationInfo.Description
if($WindowsScheduledTasks | where{$_.Description -eq $Title}){
Write-Host "Update Windows scheduled task: $Title"
SCHTASKS /DELETE /TN $Title /F
SCHTASKS /Create /TN $Title /XML $FilePathTemp
}else{
Write-Host "Adding Windows scheduled task: $Title"
SCHTASKS /Create /TN $Title /XML $FilePathTemp
}
Remove-Item -Path $FilePathTemp
}
}
}